#include "simulationunit.h"
#include "fmi2Functions.h"

#include <stdio.h>


int main(int argc, char *argv[])
{
    puts("[Simulator] *** Loading, instantiating and initiating FMU ***");
    SimulationUnit unit(L"fmu_flatone.dll", "FlatOne");
    if (!unit.isLoaded())
    {
        printf("[Simulator] ERROR: Load FMU failed. (code: %d)\n", unit.errorCode());
        return -1;
    }
    puts(unit.getTypesPlatform());
    puts(unit.getVersion());

    double timeStart = 0.0;
    double timeStep = 0.01;
    double timeEnd = 3.0;
    unit.setupExperiment(timeStart, timeEnd);

    int initValue[1] = {777};
    unit.setInteger({0}, 1, initValue);

    puts("[Simulator] *** Starting simulation ***");
    int pValueFromUnit[1];
    double t = timeStart;
    fmi2Status res = fmi2OK;
    while (t <= timeEnd)
    {
        res = unit.doStep(t, timeStep);
        if (res == fmi2Fatal)
        {
            printf("[Simulator] FATAL on doing step.");
            break;
        }
        res = unit.getInteger({0}, 1, pValueFromUnit);
        if (res == fmi2Fatal)
        {
            printf("[Simulator] FATAL on getting values.");
            break;
        }
        printf("[Simulator] Successfully got value from FMU: %d\n", pValueFromUnit[0]);
        t += timeStep;
    }
    puts("[Simulator] *** Simulation ends ***");

    return 0;
}